var wxpay = require('../../utils/pay.js')
var app = getApp()
// 语言
var util = require('../../utils/util.js')
import event from '../../utils/event'

Page({
  data: {
    statusType: ["申请售后", "处理中", "完成"], 
    relate:{
      0:'/order/after-list',
      1:'/order/process',
      2:'/order/process-finish'
    },
    currentType: 2,
    tabClass: ["", "", "", "", ""],
    //语言 - beginf
    language: '',
    //语言 - end
    bodyHeight: null,
    repeatSend: null
  },
  statusTap: function (e) {
    var obj = e;
    var count = 0;
    for (var key in obj) {
      count++;
    }
    if (count == 0) {
      var curType = 0;
    } else {
      console.log('出现Cannot read property "dataset" of undefined;这样的错误是正常的，不用管！');
      var curType = e.currentType || e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.index || 0;
    }
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.onShow();
  },

  onLoad: function (e) {
    var that = this;
    if (e.share) {
      that.setData({ share: e.share });
    }
    if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
   
    var currentType = e.currentType;
    this.data.currentType = currentType;
    if (currentType) {
      that.setData({
        currentType: currentType
      });
    }
    that.statusTap(e);
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function (e) {

    // 获取订单列表
    wx.showLoading();
    var that = this;
   
    var requestStatus = that.data.currentType;
    wx.request({
      url: app.globalData.urls + this.data.relate[requestStatus], // 售后列表
      // method:"POST",
      data: {
        // wxRequestOrderStatus: that.data.relate[requestStatus],
        // withItems: 1
      },
      header: app.getRequestHeader(),
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 200) {
          var data = res.data.data;
          let result=requestStatus==0? data.orderList:data;
          result.forEach(val=>{
            switch(val.status){
                case "after_sale_waiting_return":
                  val.status_text = "申请中"
                break;
                case "after_sale_return_accept":
                  val.status_text = "审核通过"
                break;
                case "after_sale_return_refuse":
                  val.status_text = "申请拒绝"
                break;
                case "after_sale_return_cancel":
                  val.status_text = "取消"
                break;
                case "after_sale_return_dispatch":
                  val.status_text = "已发货"
                break;
                case "after_sale_return_received":
                  val.status_text = "已收货"
                break;
                case "after_sale_return_refund":
                  val.status_text = "已退款"
                break;
                case "after_sale_return_admin_dispatch":
                  val.status_text = "商家已发货"
                break;
            }
          })
          that.setData({
            orderList:result
          });
        } else {
          that.setData({
            orderList: null,
          });
        }
        
        app.saveReponseHeader(res);
      }
    })
    var winInfo = wx.getSystemInfo({
      success: function (res) {
        var windowHeight = res.windowHeight;
        var statusBarHeight = res.statusBarHeight;
        var titleBarHeight = 0
        if (res.model.indexOf('iPhone') !== -1) {
          titleBarHeight = 44
        } else {
          titleBarHeight = 48
        }
        var query = wx.createSelectorQuery();
        query.select('.status-box').boundingClientRect()
        query.exec((res) => {
          var listHeight = res[0].height; // 获取list高度
          that.setData({ bodyHeight: windowHeight - statusBarHeight - titleBarHeight - listHeight });
        })


      }
    });
  },
  // 申请售后
  applyAfterSale(e){
    let tab = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/after-type/after-type?name=" + tab.name + "&num=" + tab.qty + "&price=" + (tab.base_grand_total||tab.grand_total) + "&pic=" + tab.item_products[0].pic + "&orderNumber=" + tab.increment_id+"&item_id="+tab.item_id
    })
  },
  // 查看详情
  showDetail(e){
    let item = e.currentTarget.dataset.item;

    let status = item.status, params = "", type = item.application_type=="退款" ? 2:1;
    
    switch (status){
      case "after_sale_waiting_return": // 等待审核
        params = "isSuccess=true&type=" + type
        break;
      case "after_sale_return_accept": // 已通过审核（填写联系人）
        params = "type=3&title=填写物流信息&client=1&id=" + item.id+"&orderId="+item.order_id
        break;
      case "after_sale_return_dispatch": // 已发货状态
        params = "isSuccess=true&status=已发货状态"
        break;
      case "after_sale_return_received": // 已收货
        params = "isSuccess=true&status=已收货"
        break;
      case "after_sale_return_refund": // 完成
        params = "isSuccess=true&isComplete=true"
        break;
      case "after_sale_return_admin_dispatch":
        params = "isSuccess=true&status=商家已发货"
      break;
    }
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?id=" + item.id + "&" + params
    })
  },
  // 去发货
  goSend(e){
    let dataset=e.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?id=" + dataset.item.id + "&type=3&title=发货&client=1&status=已成功发货&orderId="+dataset.item.order_id
    })
  },
  // 确认收货
  confrimGoods(e){
    var that = this;
    let item= e.currentTarget.dataset.item;
    wx.showModal({
      title:"是否确定收到货物",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/confirm-deliver-customer',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            id:item.id
          },
          success: function (res) {
            if(res.data.code==200){
              wx.showToast({
                title: '确认收货成功',
                icon:'none'
              })
              that.onShow();
            }else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
            
          }
        })
      }
    })
  },
  // 查看商品详情
  orderDetail(e){
    let item= e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/order-detail/order-detail?id='+item.id+"&cb=1&b=1",
    })
  },
  // 倒计时
  countDown() {
    let that = this;
    setInterval(() => {
      let result = that.data.orderList.map(val => {
        if (val.limit_time >= 1) val.limit_time--;
        val.time = util.sec_to_time(val.limit_time)
        return val;
      })
      that.setData({
        orderList: result,
      })
    }, 800)
  },
})